package com.dykj.fireFighting.web.devicemanger;

import com.dykj.fireFighting.api.socialUnit.devicemanger.dto.DeviceRescueDto;
import com.dykj.fireFighting.api.socialUnit.devicemanger.entity.DeviceRescue;
import com.dykj.fireFighting.api.socialUnit.devicemanger.service.IDeviceRescueService;
import com.dykj.fireFighting.common.core.annotation.AutoLog;
import com.dykj.fireFighting.common.core.api.CommonResult;
import com.dykj.fireFighting.common.core.exception.BaseException;
import com.dykj.fireFighting.common.core.vo.PageVo;
import com.dykj.fireFighting.web.base.shiro.ShiroUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.Date;


/**
 * 设备管理-救援设备表接口
 *
 * @Author lch
 * @Date 2021-06-28 14:41:36
 */
@Slf4j
@Api(value = "设备管理-救援设备表接口", tags = {"设备管理-救援设备表接口"})
@RestController
@RequestMapping("/sms/deviceRescue")
public class DeviceRescueController {

    @DubboReference
    private IDeviceRescueService deviceRescueService;


    /**
     * 获取单位id
     *
     * @return String
     */
    public String getEnterpriseId() {
        try {
            return Long.toString(ShiroUtil.getUser().getEnterpriseId());
        } catch (NullPointerException e) {
            e.printStackTrace();
            throw new BaseException("请登录系统");
        }
    }

    /**
     * 获取用户名称
     *
     * @return String
     */
    public String getName() {
        try {
            return ShiroUtil.getUser().getName();
        } catch (NullPointerException e) {
            e.printStackTrace();
            throw new BaseException("请登录系统");
        }
    }

    /**
     * 设备管理-救援设备表列表
     *
     * @return
     */
    @AutoLog(value = "设备管理-救援设备表-分页列表查询")
    @ApiOperation(value = "设备管理-救援设备表分页列表", notes = "设备管理-救援设备表分页列表")
    @GetMapping("/list")
    @RequiresPermissions("sms/deviceRescue/list")
    public CommonResult<PageVo<DeviceRescue>> treeGrid(DeviceRescueDto deviceRescueDto) {
        deviceRescueDto.setUnitId(getEnterpriseId());
        PageVo<DeviceRescue> page = deviceRescueService.pageList(deviceRescueDto);
        return CommonResult.success(page);
    }

    /**
     * 获取设备管理-救援设备表信息
     */
    @AutoLog(value = "设备管理-救援设备表-获取设备管理-救援设备表信息")
    @ApiOperation(value = "获取设备管理-救援设备表信息", notes = "获取设备管理-救援设备表信息")
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", dataType = "String", required = true)
    @GetMapping("/info/{id}")
    @RequiresPermissions("sms/deviceRescue/info")
    public CommonResult<DeviceRescue> info(@PathVariable("id") String id) {
        DeviceRescue deviceRescue = deviceRescueService.getById(id);
        return CommonResult.success(deviceRescue);
    }

    /**
     * 保存设备管理-救援设备表信息
     */
    @AutoLog(value = "设备管理-救援设备表-保存设备管理-救援设备表信息")
    @ApiOperation(value = "保存设备管理-救援设备表信息", notes = "保存设备管理-救援设备表信息")
    @PostMapping("/save")
    @RequiresPermissions("sms/deviceRescue/save")
    public CommonResult save(@Valid @RequestBody DeviceRescue deviceRescue) {
        try {
            deviceRescue.setCreateTime(new Date());
            deviceRescue.setUpdateTime(new Date());
            if (StringUtils.isBlank(getEnterpriseId())) {
                throw new BaseException("请登录系统");
            } else {
                deviceRescue.setUnitId(getEnterpriseId());
                deviceRescue.setCreateBy(getName());
            }
            deviceRescueService.save(deviceRescue);
            return CommonResult.success("添加成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("添加失败，请联系管理员");
        }
    }

    /**
     * 修改设备管理-救援设备表信息
     */
    @AutoLog(value = "设备管理-救援设备表-修改设备管理-救援设备表信息")
    @ApiOperation(value = "修改设备管理-救援设备表信息", notes = "修改设备管理-救援设备表信息")
    @PostMapping("/update")
    @RequiresPermissions("sms/deviceRescue/update")
    public CommonResult update(@Valid @RequestBody DeviceRescue deviceRescue) {
        try {
            deviceRescue.setUpdateTime(new Date());
            deviceRescue.setUpdateBy(getName());
            deviceRescueService.updateById(deviceRescue);
            return CommonResult.success("修改成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("添加失败，请联系管理员");
        }
    }

    /**
     * 删除设备管理-救援设备表信息
     */
    @AutoLog(value = "设备管理-救援设备表-删除设备管理-救援设备表信息")
    @ApiOperation(value = "删除设备管理-救援设备表信息", notes = "删除设备管理-救援设备表信息")
    @ApiImplicitParam(paramType = "query", name = "ids", value = "ID数组", dataType = "String", required = true, allowMultiple = true)
    @PostMapping("/delete")
    @RequiresPermissions("sms/deviceRescue/delete")
    public CommonResult delete(@RequestBody String[] ids) {
        try {
            deviceRescueService.removeByIds(Arrays.asList(ids));
            return CommonResult.success("删除成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("添加失败，请联系管理员");
        }
    }
}
